<template>
  <div>
    <h3>登录页</h3>
    <van-form>
      <van-field
        v-model="accountName"
        name="用户名"
        label="用户名"
        placeholder="请输入用户名"
      />
      <van-field
        v-model="password"
        type="password"
        name="密码"
        label="密码"
        placeholder="请输入密码"
      />
      <div class="mt20">
        <van-button
          round
          block
          type="info"
          @click="handleLogin"
        >提交</van-button>
      </div>
    </van-form>
  </div>
</template>

<script>
import { login } from '@/api/login'
import md5 from 'md5'
import { mapMutations } from 'vuex'

export default {
  data() {
    return {
      accountName: 'lqq',
      password: '123456'
    }
  },
  created() {
    // this.$bus.$emit('error')
    setTimeout(() => {
      this.$bus.$emit('ready')
    }, 200)
  },
  methods: {
    ...mapMutations('login', ['UPDATE_LOGIN']),
    async handleLogin() {
      const params = {
        accountName: this.accountName,
        password: md5(this.password)
      }
      const res = await login(params)
      console.log(res)
      if (res.success) {
        this.$toast('登录成功')
        this.UPDATE_LOGIN(true)
        this.$router.replace('/order')
      }
    }
  }
}
</script>
